/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.support.values;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class defines values of the <i>'a list</i> type.
 *
 * @param <T> type of list elements
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
public final class List<T extends ToValue> implements ToValue, Iterable<T> {

    /** The actual value. */
    private final java.util.List<T> value;

    /**
     * Constructs an empty list.
     */
    public List() {
        this.value = new LinkedList<T>();
    } // end empty constructor

    /**
     * Constructs a list.
     * @param that list to copy - should not be <tt>null</tt>
     */
    public List(final java.util.List<T> that) {
        assert that != null : "null that";
        this.value = new LinkedList<T>(that);
    } // end constructor(List<T>)

    /**
     * Constructs a list.
     * @param elements elements to put in the list - should not be <tt>null</tt>
     */
    public List(final T... elements) {
        assert elements != null : "null elements";
        this.value = new LinkedList<T>();
        for (T e : elements) {
            this.value.add(e);
        } // end for
    } // end constructor(T...)

    /**
     * {@inheritDoc}
     */
    @Override
    public Iterator<T> iterator() {
        return Collections.unmodifiableList(this.value).iterator();
    } // end method 'iterator()'

    /**
     * {@inheritDoc}
     */
    @Override
    public Value toValue() {
        Value res = Value.EMPTY_LIST;
        final ListIterator<T> it = this.value.listIterator(this.value.size());
        while (it.hasPrevious()) {
            final Block cons = Block.createBlock(Block.TAG_CONS,
                                                 it.previous().toValue(),
                                                 res);
            res = Value.createFromBlock(cons);
        } // end while
        return res;
    } // end method 'toValue()'

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return this.value.hashCode();
    } // end method 'hashCode()'

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof List) {
            final List<?> that = (List<?>) obj;
            return this.value.equals(that.value);
        } else {
            return false;
        } // end if/else
    } // end method 'equals(Object)'

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("[");
        boolean first = true;
        for (T e : this.value) {
            if (first) {
                first = false;
            } else {
                sb.append("; ");
            } // end if/else
            sb.append(e.toString());
        } // end for
        sb.append("]");
        return sb.toString();
    } // end method 'toString()'

} // end class 'List'
